<?php

class data_base
{
    protected $_db = null;

    public function __construct()
    {
        $this->_db = new lib_base_db();

    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据ID获取记录
     * @param int $id
     * @return array or false
     */
    function fetch_by_id ( $id )
    {
        if ( !is_numeric( $id )  ) return FALSE;
        $sql = " SELECT * FROM `{$this->_table}` WHERE id = '{$id}' LIMIT 1 ";
        return $this->_db->fetchRow( $sql );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据ID获取记录
     * @param int $id,$field;
     * @return array or false
     */
    function fetch_one_field_by_id ( $id, $field )
    {
        if ( !is_numeric( $id )  ) return FALSE;
        $sql = " SELECT {$field} FROM `{$this->_table}` WHERE id = '{$id}' LIMIT 1 ";
        return $this->_db->fetchOne( $sql, $field );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据name字段获取记录
     * @param int $id
     * @return array or false
     */
    function fetch_by_name ( $name )
    {
        if ( $name == ''  ) return FALSE;
        $sql = " SELECT * FROM `{$this->_table}` WHERE name = '{$name}' LIMIT 1 ";
        return $this->_db->fetchRow( $sql );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据条件获取一条记录
     * @param string $condition
     * @return array or false
     */
    function fetch_one_by_condition ( $condition )
    {
        if ( empty( $condition ) ) return FALSE;
        $sql = " SELECT * FROM `{$this->_table}` WHERE {$condition} LIMIT 1 ";
        return $this->_db->fetchRow( $sql );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据条件获取所有记录
     * @param string $condition
     * @return array or false
     */
    function fetch_all_by_condition ( $condition )
    {
        if ( empty( $condition ) ) return FALSE;
        $sql = " SELECT * FROM `{$this->_table}` WHERE {$condition} ";
        return $this->_db->fetchAll( $sql );
    }

    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据条件获取所有记录
     * @param string $condition $order $limit
     * @return array or false
     */
    function fetch_all_by_condition_order_limit ( $condition, $order = '', $limit = '' )
    {
        if ( empty( $condition ) ) return FALSE;
        $sql = " SELECT * FROM `{$this->_table}` WHERE {$condition} ";
        if ( $order == '' )
        {
           $sql .= " ORDER BY id DESC ";
        }
        else
        {
            $sql .= " ORDER BY {$order}  ";
        }
        if ( $limit != '' )
        {
            $sql .= " LIMIT {$limit} ";
        }
        return $this->_db->fetchAll( $sql );
    }
    /**
    * @author shanghui@leju.sina.com.cn 2011-03-21
    * 根据条件获取一条记录
    * @param string $condition, $field
    * @return array or false
    */
    function fetch_one_field_by_condition ( $condition, $field )
    {
        if ( empty( $condition ) || empty( $field ) ) return FALSE;
        $sql = " SELECT {$field} FROM `{$this->_table}` WHERE {$condition} LIMIT 1 ";
        return $this->_db->fetchOne( $sql, $field );

    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据ID更新记录
     * @param int $id
     * @param array $data
     * @return int or false
     */
    function update_by_id ( $id, $data )
    {
        if ( !is_numeric( $id ) || !is_array( $data )  ) return false;
        $condition = " id = '{$id}' ";

        return $this->_db->update( $this->_table, $data, $condition );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据ID更新记录
     * @param string $condition
     * @param array $data
     * @return int or false
     */
    function update_by_condition ( $data, $condition )
    {
        if ( empty( $condition ) || !is_array( $data ) ) return FALSE;
        return $this->_db->update( $this->_table, $data, $condition );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据ID删除记录
     * @param int $id
     * @return true or false
     */
    function delete_by_id ( $id )
    {
        if ( !is_numeric( $id )  ) return FALSE;
        $sql = " DELETE FROM `{$this->_table}` WHERE id = '{$id}' ";
        return $this->_db->exec( $sql );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 根据条件删除记录
     * @param string $condition
     * @return true or false
     */
    function delete_by_condition ( $condition )
    {
        if ( empty( $condition ) ) return FALSE;
        $sql = " DELETE FROM `{$this->_table}` WHERE {$condition} ";
        return $this->_db->exec( $sql );
    }
    /**
     * @author shanghui@leju.sina.com.cn 2011-03-21
     * 插入记录
     * @param array $data
     * @return int 插入行的ID or false
     */
    function insert_row( $data )
    {
        if ( !is_array( $data ) ) return FALSE;
        return $this->_db->insert( $this->_table, $data );
    }

    /**
     * @author mingyu@leju.sina.com.cn 2011-04-05
     * 根据条件获取记录数
     * @param string $condition
     * @return count
     */
    function fetch_count_by_condition ( $condition )
    {
        if ( empty( $condition ) ) return FALSE;
        $sql = "SELECT COUNT(*) as count FROM {$this->_table} WHERE  {$condition}";
        return $this->_db->fetchOne($sql,'count');
    }
}
